package com.yimi.yinhepda.db.dao;

import android.text.TextUtils;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.pda.uce.commom.util.SpUtil;
import com.uce.pda.MyAppLication;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.FuncBtnTable;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: lj
 * Date: 2019/10/8 17:55
 * Description:
 */
public class FuncBtnTableDao extends BaseDao<FuncBtnTable, Long> {

    public FuncBtnTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), FuncBtnTable.class);
    }

    public List<FuncBtnTable> findByCommId(int isCommUse) {
        try {
            QueryBuilder<FuncBtnTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("isCommUse", isCommUse);
            if (TextUtils.isEmpty(SpUtil.getInstance().get("tokenId"))){
                where.and().eq("funcType", 0);
            }

            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void createOrUpdate(List<FuncBtnTable> btnTableList) {
        try {
            for (FuncBtnTable item : btnTableList) {
                QueryBuilder<FuncBtnTable, Long> queryBuilder = dao.queryBuilder();
                Where where = queryBuilder.where();
                where.eq("nameCode", item.getNameCode());
                FuncBtnTable btnData = queryBuilder.queryForFirst();
                if (btnData == null) {
                    //不存在这个功能按钮则插入
                    this.insert(item);
                } else {
//                    btnData = FuncBtnTable.updateDataTable(btnData, item);
//                    //存在这个功能按钮则更新
//                    this.update(btnData);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
